<?php

namespace App\Http\Controllers\User;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Modules\Car\Entities\CarOrder;

// 用户统计
class UserStatisticController extends Controller
{
    use ResponseData;
    // 出勤统计
    public function attendance(CarOrder $model, Request $request){
        $size = $request->input('size',10);
        $page = $request->input('page',1);
        $offset = ($page-1)*$size;

        //出车
        $map1 = $model->query()
            ->select('department','order_sn','plate_number',
                'send_employer_nickname as name',
                'send_employer_id as id',
                'fetch_time as time');
        if($request->filled('start')&&$request->filled('end')){
            $map1 = $map1->whereBetween('fetch_time',[$request->start,$request->end]);
        }
        if($request->filled('name')){
            $map1 = $map1->where('send_employer_nickname',$request->name);
        }
        if($request->filled('plate_number')){
            $map1 = $map1->where('plate_number',$request->plate_number);
        }
        if($request->filled('order_sn')){
            $map1 = $map1->where('order_sn',$request->order_sn);
        }
        if($request->filled('department')){
            $map1 = $map1->where('department',$request->department_id);
        }
        $map1 = $map1->whereNotNull('fetch_time')
            ->orderByDesc('send_employer_time')
            ->offset($offset)
            ->limit($size)
            ->get();
        // 还车
        $map2 =$model->replicate()
            ->select('department','order_sn','plate_number',
                'return_employer_nickname as name','return_employer_id as id','fact_return_over_at as time')
            ->whereNotNull('fact_return_over_at');
        if($request->filled('start')&&$request->filled('end')){
            $map2 = $map2->whereBetween('fact_return_over_at',[$request->start,$request->end]);
        }
        if($request->filled('name')){
            $map2 = $map2->where('return_employer_nickname',$request->name);
        }
        if($request->filled('plate_number')){
            $map2 = $map2->where('plate_number',$request->plate_number);
        }
        if($request->filled('order_sn')){
            $map2 = $map2->where('order_sn',$request->order_sn);
        }
        if($request->filled('department')){
            $map2 = $map2->where('department',$request->department_id);
        }
        $map2 = $map2->orderBy('return_employer_id')
            ->offset($offset)
            ->limit($size)
            ->get();
        $total1 = $model->replicate()->whereNotNull('fetch_time')->count('id');
        $total2 = $model->replicate()->whereNotNull('fact_return_over_at')->count('id');
        $map1 = $map1->map(function($item,$key){
            $item->lease_type= 1;
            return $item;
        });
        $map2 = $map2->map(function($item,$key){
            $item->lease_type= 2;
            return $item;
        });
//        $map1 = $map1->put('lease_type',1);
//        $map2 = $map2->put('lease_type',2);
        $merged = $map1->mergeRecursive($map2);
        $total = $total1+$total2;
        $data = $merged->sortBy('time', SORT_NATURAL)->values();
        return $this->success($data,200,$total);
    }
    public function back($data){
        return  $data->reject(function ($user) {
            return $user->return_employer_id == 0;
        })->map(function ($user) {
            $user->lease_return  = 2;
            return $user;
        });
//        return $res->all();
    }
    public function go($data){
        return  $data->reject(function ($user) {
            return $user->send_employer_id == 0;
        })->map(function ($user) {
             $user->lease_send  = 1;
            return $user;
        });
    }
}
